

import attention from '@/script/attention.js'
/**
  * 根据经纬度计算两点之间距离(m)
  * @param {latitude, longitude} point    当前位置点
  * @param {latitude, longitude} marker   待计算点
	*/
export const getDistance = (point, marker) => {
	let {latitude, longitude} = marker
	let latitude2 = point.latitude;
	let longitude2 = point.longitude;
	let rad1 = latitude * Math.PI / 180.0;
	let rad2 = latitude2 * Math.PI / 180.0;
	let a = rad1 - rad2;
	let b = longitude * Math.PI / 180.0 - longitude2 * Math.PI / 180.0;
	let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) *
		Math.cos(
			rad2) * Math.pow(
			Math.sin(b / 2), 2)));
	s = s * 6378.137; 
	// s = Math.round(s * 10000) / 10000;  公里
	s = Math.round(s * 10000) / 10000;
	s = s.toFixed(2).toString();
	return parseFloat(s)
}

export const getAddress = ([lng, lat], fn, marker) => {	
	const geocoder = new AMap.Geocoder({
		city: "010", //城市设为北京，默认：“全国”
		radius: 1000 //范围，默认：500
	})
	geocoder.getAddress([lng, lat], (status, result) => {
		if(status == 'complete'){
			let {formattedAddress, addressComponent = {}} = result.regeocode
			let {district, city, province} = addressComponent || {}, distace = 0
			if(marker){
				let {latitude, longitude} = marker
				let rad1 = latitude * Math.PI / 180.0;
				let rad2 = lat * Math.PI / 180.0;
				let a = rad1 - rad2;
				let b = longitude * Math.PI / 180.0 - lng * Math.PI / 180.0;
				let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) *
					Math.cos(
						rad2) * Math.pow(
						Math.sin(b / 2), 2)));
				s = s * 6378.137;
				// s = Math.round(s * 10000) / 10000;  公里
				s = Math.round(s * 10000) / 10000;  //千米
				distace = s.toFixed(2).toString();
			}
			return fn({lat, lng, district, city, province, distace, address: formattedAddress})
		}else{
			attention.noIcon(status + '22')
		}
	})
}

